using System.Windows.Forms;
using RollManager.Forms.ActionPanels;
using RollManager.Library;

namespace RollManager.Forms.MainPanels
{
    public partial class PerformActionsPanel : UserControl, MainPanel
    {
        #region Properties
        private Action Action
        {
            get
            {
                return (Action) actionList.SelectedItem;
            }
        }

        private DevLevel DevLevel
        {
            get
            {
                return (DevLevel) devLevelList.SelectedItem;
            }
        }

        private ListBox.SelectedObjectCollection SubActions
        {
            get
            {
                return targetList.SelectedItems;
            }
        }
        #endregion

        public PerformActionsPanel()
        {
            InitializeComponent();
        }

        public override void Refresh()
        {
            base.Refresh();

            // Setup list of dev levels.
            devLevelList.DataSource = Database.GetDevLevels();
            devLevelList.DisplayMember = ItemBase.NAME_PROPERTY;
            devLevelList.SelectedIndex = -1;

            actionList.DataSource = null;
            targetList.DataSource = null;
        }

        private void devLevelList_Click( object sender, System.EventArgs e )
        {
            if( DevLevel == null )
            {
                return;
            }

            // Show actions for dev level.
            actionList.DataSource = Database.GetActions( DevLevel.Id );
            actionList.DisplayMember = ItemBase.NAME_PROPERTY;

            if( actionList.Items.Count == 1 )
            {
                actionList.SelectedIndex = 0;
                actionList_Click( null, null );
            }
            else
            {
                actionList.SelectedIndex = -1;
                targetList.DataSource = null;
            }
            
        }

        private void actionList_Click( object sender, System.EventArgs e )
        {
            if( Action == null )
            {
                return;
            }

            // Show actions for dev level.
            targetList.DataSource = Database.GetSubActions( Action.Id );
            targetList.DisplayMember = ItemBase.NAME_PROPERTY;
            targetList.SelectedIndex = -1;
        }

        private void targetList_DoubleClick( object sender, System.EventArgs e )
        {
            foreach( SubAction subAction in SubActions )
            {
                ActionDialog.StartAction( DevLevel, Action, subAction, false );
            }
        }

        public void Save()
        {
        }

        private void targetList_KeyPress( object sender, KeyPressEventArgs e )
        {
            if( e.KeyChar == (int) Keys.Enter )
            {
                targetList_DoubleClick( sender, e );
            }
        }

        private void targetMenu_Opening( object sender, System.ComponentModel.CancelEventArgs e )
        {
            if( SubActions.Count < 1 )
            {
                e.Cancel = true;
                return;
            }
        }

        private void showHistoryToolStripMenuItem_Click( object sender, System.EventArgs e )
        {
            foreach( SubAction subAction in SubActions )
            {
                Common.ShowHistory( DevLevel, Action, subAction );
            }
        }
    }
}